package common

import (
	"time"
)

// 时间戳对比，是否在同一天 传参：时间戳1，时间戳2，偏移时间(秒)
func InSameDay(t1, t2 int64, offsetSec int64) bool {
	t1 -= offsetSec
	t2 -= offsetSec
	y1, m1, d1 := time.Unix(t1, 0).Date()
	y2, m2, d2 := time.Unix(t2, 0).Date()
	return y1 == y2 && m1 == m2 && d1 == d2
}

// 时间戳对比，是否在同一周 传参：时间戳1，时间戳2，偏移时间(秒)
func InSameWeek(t1, t2 int64, offsetSec int64) bool {
	t1 -= offsetSec
	t2 -= offsetSec
	y1, w1 := time.Unix(t1, 0).ISOWeek()
	y2, w2 := time.Unix(t2, 0).ISOWeek()
	return y1 == y2 && w1 == w2
}
func GetWeekStart(now time.Time, offsetSec int64) time.Time {
	now = now.Add(-time.Duration(offsetSec) * time.Second)
	//y, m := now.ISOWeek()
	for now.Weekday() != time.Monday {
		now = now.Add(-24 * time.Hour)
	}
	y, m, d := now.Date()
	return time.Date(y, m, d, 0, 0, int(offsetSec), 0, now.Location())
}

// 获取now的一天开始
func GetDayStart(now time.Time, offsetSec int64) time.Time {
	now = now.Add(-time.Duration(offsetSec) * time.Second)
	y, m, d := now.Date()
	return time.Date(y, m, d, 0, 0, int(offsetSec), 0, now.Location())
}

// 时间戳对比，是否在同一月 传参：时间戳1，时间戳2，偏移时间(秒)
func InSameMonth(t1, t2 int64, offsetSec int64) bool {
	t1 -= offsetSec
	t2 -= offsetSec
	y1, m1, _ := time.Unix(t1, 0).Date()
	y2, m2, _ := time.Unix(t2, 0).Date()
	return y1 == y2 && m1 == m2
}

// 计算两个时间戳 相差天数
func GetOffDay(begUnix, nowUnix int64, offsetSec int64) int32 {
	begUnix -= offsetSec
	nowUnix -= offsetSec

	var diffDays int32 = 0
	secDiff := nowUnix - begUnix
	var SecondsOfDay int64 = 24 * 60 * 60
	isNegtive := secDiff < 0
	if secDiff/SecondsOfDay != 0 { //不在24h内的 就是 超过24h; 应该考虑负数的情况的!! fixed: secDiff > SecondsOfDay
		tmpDays := int(secDiff / SecondsOfDay)
		begUnix += int64(tmpDays) * SecondsOfDay //补偿多少天 并且计算天数
		diffDays += int32(tmpDays)
	}

	st := time.Unix(begUnix, 0)
	et := time.Unix(nowUnix, 0)
	dateFormatTpl := "20060102"
	if st.Format(dateFormatTpl) != et.Format(dateFormatTpl) { //不是同一天 就+1
		if isNegtive { //现在在过去 需要到未来
			diffDays -= 1
		} else { //现在在未来 根据过去计算天数,从0开始
			diffDays += 1
		}
	}
	if diffDays >= 0 {
		diffDays += 1
	}
	return diffDays
}
func UnixToZeroTime(orgUnix int64, offsetSec int64) time.Time {
	theTime := time.Unix(orgUnix, 0)
	return time.Date(theTime.Year(), theTime.Month(), theTime.Day(), 0, 0, 0, 0, time.Local).Add(time.Duration(offsetSec) * time.Second)
}

func GetSrvOpenDay(svrOpenTimeUnix, nowUnix int64, offsetSec int64) int32 {
	svrOpenTimeUnix -= offsetSec
	nowUnix -= offsetSec
	srvOpenTime := time.Unix(svrOpenTimeUnix, 0)
	nowTime := time.Unix(int64(nowUnix), 0)
	// 服务器开启5点
	svrOpenZeroTime := time.Date(srvOpenTime.Year(), srvOpenTime.Month(), srvOpenTime.Day(), 0, 0, 0, 0, time.Local) //.Add(time.Duration(offsetSec) * time.Second)
	nowZeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
	day := (nowZeroTime.Unix()-int64(svrOpenZeroTime.Unix()))/86400 + 1
	if day <= 0 {
		day -= 1
	}

	//log.Printf("SrvOpenDay=%v NowDay=%v", svrOpenZeroTime.Format(time.DateTime), nowZeroTime.Format(time.DateTime))
	return int32(day)
}

func GetCurWeekStart(curtime time.Time) time.Time {
	// 拿今天是周几
	weekNum := GetWeekNumberIn1to7[int](curtime)
	y, m, d := curtime.Date()
	return time.Date(y, m, d-weekNum, 5, 0, 0, 0, curtime.Location())
}

// 获取下周开始时间
func GetNextWeekStart(curtime time.Time) time.Time {
	curWeekStart := GetCurWeekStart(curtime)
	y, m, d := curWeekStart.Date()
	return time.Date(y, m, d+7, 5, 0, 0, 0, curWeekStart.Location())
}

// 根据传入的当前时间 获取距离下一周开始的时间间隔
func GetNextWeekIntervalMillSec(curtime time.Time) (result int64) {
	// 拿今天是周几
	weekNum := GetWeekNumberIn1to7[int](curtime)
	// 拿下周开始时间 5点为准
	nextWeekStartTime := curtime.AddDate(0, 0, 7-weekNum)
	y, m, d := nextWeekStartTime.Date()
	return time.Date(y, m, d, 5, 0, 0, 0, nextWeekStartTime.Location()).UnixMilli() - curtime.UnixMilli()
}

// 根据时间 获得 周几（1~7）
func GetWeekNumberIn1to7[T int32 | int](curtime time.Time) T {
	week := curtime.Weekday()
	if week == 0 {
		week = 7
	}
	return T(week)
}
